import { request } from '@umijs/max';

// 认证相关的API接口
export interface LoginParams {
  login_id: string;  // 用户名、邮箱或手机号
  password: string;
  remember_me?: boolean;
}

export interface RegisterParams {
  username: string;
  email: string;
  phone: string;
  password: string;
  password_confirm: string;
  first_name?: string;
  last_name?: string;
  nickname?: string;
}

export interface AuthResponse {
  success: boolean;
  message: string;
  data?: {
    user: {
      id: string;
      username: string;
      email: string;
      phone: string;
      nickname?: string;
      first_name?: string;
      last_name?: string;
    };
    token: string;
    expires_in?: number;
  };
  errors?: any;
}

// 用户登录
export async function login(params: LoginParams) {
  return request<AuthResponse>('/api/accounts/login/', {  // 添加末尾斜杠
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

// export async function login(params: LoginParams) {
//   // 临时直接请求后端，绕过代理
//   return request<AuthResponse>('http://localhost:8001/api/accounts/login/', {
//     method: 'POST',
//     data: params,
//   });
// }

// 用户注册
export async function register(params: RegisterParams) {
  return request<AuthResponse>('/api/accounts/register/', {  // 修复重复的/api
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

// 用户登出
export async function logout() {
  return request('/api/accounts/logout/', {
    method: 'POST',
  });
}

// 获取当前用户信息
export async function getCurrentUser() {
  return request('/api/accounts/users/me/', {
    method: 'GET',
  });
}
